home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xmeasure / timer.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  73 lines

  1. /*==================================================================
  2.  *      File :          timer.c
  3.  *      Package:        Xmeasure
  4.  * 
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *  All rights granted to University of Illinois Board of Regents.
  9.  *==================================================================*/
  10.  
  11. /* Useful timer functions */
  12.  
  13. #include <stdio.h>
  14. #include <sys/time.h>
  15. #include "perf.h"
  16.  
  17. /*
  18.  * Convenient functions for measuring elapsed time
  19.  */
  20. static double sync_time    = 0.0;    /* Synchronization delay */
  21. static double initial_time = 0.0;
  22.  
  23. SetSyncTime(time)
  24. double time;
  25. { sync_time = time; }
  26.  
  27. InitTimer()
  28. { initial_time = gettime(); }
  29.  
  30. double TimeSpent() {
  31.     double retval;
  32.     retval = gettime() - initial_time - sync_time;
  33.     if (retval >= 0.0)
  34.      return(retval);
  35.     else return(0.0);
  36. }
  37.  
  38. double gettime()     /* Return time of the day in seconds since the */
  39. {            /* beginning of this program                   */
  40.     static long initial_sec  = -1, initial_usec = -1;
  41.     struct timeval tp;
  42.     struct timezone tzp;
  43.  
  44.     gettimeofday(&tp, &tzp);
  45.     if (initial_sec == -1) {
  46.     initial_sec  = tp.tv_sec;
  47.     initial_usec = tp.tv_usec;
  48.     return(0.0);
  49.     }
  50.     return( (double) (tp.tv_sec -initial_sec) +
  51.         ((tp.tv_usec - initial_usec) / 1000000.0) );
  52. }
  53.  
  54. PrintTime(fp)
  55. FILE *fp;
  56. {   struct timeval tp;
  57.     struct timezone tzp;
  58.  
  59.     gettimeofday(&tp, &tzp);
  60.     fprintf(fp, "%s\n", ctime (&tp.tv_sec) );
  61. }
  62.  
  63. double getabstime()     /* Return time of the day in seconds */
  64. {
  65.     struct timeval tp;
  66.     struct timezone tzp;
  67.  
  68.     gettimeofday(&tp, &tzp);
  69.     return( (double) tp.tv_sec + (tp.tv_usec / 1000000.0) );
  70.  
  71. }
  72.  
  73.